package cn.gan.loveofsoul.exception;

import cn.gan.loveofsoul.commons.result.Result;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


/**
 * @Description:
 * @Author: ganwh
 * @Since: 2023/10/9 16:04
 */
@ControllerAdvice
public class ResponseBodyHandle implements ResponseBodyAdvice<Result> {
    private static final Logger logger = LoggerFactory.getLogger(ResponseBodyHandle.class);

    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //支持所有方法
        return true;
    }

    @Override
    public Result beforeBodyWrite(Result body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        String methodName = returnType.getMethod().getName();
        logger.info("{}方法返回值={}", methodName, JSON.toJSONString(body));
        return body;
    }
}
